package com.longrise.android.byjk.plugins.aboutme.personalInfo.person_honor;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2018/3/8.
 * 添加个人荣誉
 */

public class AddPersonHonorActivity extends BaseActivity2<AddPersonHonorPresenter> implements AddPersonHonorContract.View, TextWatcher {

    public static final String HONORDATA = "honordata";
    public static final String HONORDATAID = "honordataid";
    private EditText mEt;
    private String save;
    private String mEt1Length;
    private String mHonordesc;
    private String mHonorId;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_add_person_honor;
    }

    @Override
    public void initView() {
        mEt = (EditText) findViewById(R.id.et_add_person_honor);
        setToolbarTitle(AppUtil.getString(R.string.person_honor));
        save = AppUtil.getString(R.string.savepic);
        initEvent();
        getExtraData();
        setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
    }

    public void initEvent(){
        mEt.addTextChangedListener(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    @Override
    public void onToolbarRightTvClick() {
        super.onToolbarRightTvClick();
        if(TextUtils.isEmpty(mHonorId)){
            mPresenter.addPersonHonorData(mEt1Length);
        }else{
            mPresenter.modifyPersonHonorData(mEt1Length,mHonorId);
        }
    }

    public void getExtraData() {
        Intent intent = getIntent();
        mHonordesc = intent.getStringExtra(HONORDATA);
        mHonorId = intent.getStringExtra(HONORDATAID);
        Control.setPersonHonor(mHonordesc);
        mEt.setText(Control.getPersonHonor());
        if (!TextUtils.isEmpty(Control.getPersonHonor())) {
            mEt.setSelection(Control.getPersonHonor().length());
        }
        softKey(mEt);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        mEt1Length = mEt.getText().toString();

        if(TextUtils.isEmpty(mEt1Length)){

            setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
        }else {
            if(mEt1Length.equals(Control.getPersonHonor())){
                setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
            }else{
                setToolbarRightTextTitle(save);
            }
        }
    }

    @Override
    public void refreshData(EntityBean bean) {
        PersonHonorEvent event = new PersonHonorEvent();
        event.setRefresh(true);
        EventBus.getDefault().post(event);
        finish();
    }

    private void softKey(EditText editText) {
        try {
            editText.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setVisible(boolean isVisible){
        if(isVisible) {
            setToolbarRightTextTitle(save);
        }else{
            setToolbarRightTextTitleUnable(save, Color.parseColor("#FFB3B3B3"));
        }
    }
}
